﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using gpsmid_wince.util;

namespace gpsmid_wince {
	class MapLayerDat : MapLayer {
		internal Tile[] tiles;

		public MapLayerDat(String file) {
			tiles = new Tile[6];
            DictReader dr = new DictReader();
#if(!DEBUG)
			DictReader.basename = System.IO.Path.GetDirectoryName(file);
#else
#if(DESK)
            DictReader.basename = @"gpsmid-files\";
#else
			DictReader.basename = @"E:\TMP\Osm2GpsMid\data\";
#endif
#endif
			
			Legend.readLegend();
			
			for(sbyte i=0; i<=4; i++) tiles[i] = dr.readData(i);		
		}


		Font font = new Font("Tahoma", 8f, FontStyle.Regular);
		Brush brBlack = new SolidBrush(Color.Black);
		PaintContext pc = new PaintContext();
		override public void render(Graphics g) {			
			g.Clear(Color.FromArgb((int)(0xFF000000 | Legend.COLORS[Legend.COLOR_MAP_BACKGROUND ]) ));
			pc.p = cProj;
			pc.g = g;

			SingleTile.countDrawn = 0;
			Way.countDrawn = 0;
			float scale = 6e+10f / cProj.getScalePPR();
			int minTile = Legend.scaleToTile( (int)scale );
			try {
				for(int i=0; i<=minTile; i++)
				tiles[i].paint(pc, Tile.LAYER_ALL);
			} catch(Exception e) {
				g.DrawString(e.StackTrace, font, brBlack, 30f, 50f);
			}
			g.DrawString("Scale:"+(int)scale, font, brBlack,  30f,30f); 
			g.DrawString("Tiles:"+SingleTile.countDrawn+"; Ways:"+Way.countDrawn, font, brBlack, 30f, 40f);
		}

	}
}
